package com.itheima.Utils;

import com.auth0.jwt.JWT;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class JwtUtils {
    @Value("${jwt.secret}")
    private String jwtSecret;
    @Value("${jwt.timeout}")
    private long timeout;

    public String generateJwt(Map<String,Object> claims) {
        String jwt = Jwts.builder().setClaims(claims)
                .setClaims(claims)
                .signWith(SignatureAlgorithm.ES256, jwtSecret)
                .setExpiration(new Date(System.currentTimeMillis() + timeout * 60 * 1000)).compact();
        return jwt;
    }

    //解析令牌
    public Map<String, Object> parseJwt(String jwt) {
        Claims claims = Jwts.parser()
                .setSigningKey(jwtSecret)
                .parseClaimsJws(jwt)
                .getBody();
        return  claims;
    }
}
